﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace SimpComm
{
    static public partial class ElementExtensions
    {
        public static MvcHtmlString Niko_HT_Radio<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"control-group\">");
            sb.Append("<label class=\"control-label\">");
            var LabelHtmlString = LabelExtensions.LabelFor<TModel, TProperty>(htmlHelper, expression);
            sb.Append(LabelHtmlString);
            sb.Append("</label>");
            sb.Append("<div class=\"controls\">");
            sb.Append(" <div class=\"input-prepend\">");
            var HiddenHtmlString = InputExtensions.HiddenFor<TModel, TProperty>(htmlHelper, expression, (IDictionary<string, object>)null);
            sb.Append(HiddenHtmlString);
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
            var name = metadata.PropertyName;

            sb.Append("<div class='btn-group' style='margin: 0 0 10px 0;' id='" + name + "_Radio' data-toggle='buttons-radio'>");
            sb.Append("<button class='btn' value='True'>是</button>");
            sb.Append("<button class='btn' value='False'>否</button>");
            sb.Append("</div>");
            sb.Append("<p class='help-block'>");
            var validat = ValidationExtensions.ValidationMessageFor<TModel, TProperty>(htmlHelper, expression, (string)null,
                                                                         (IDictionary<string, object>)
                                                                         new RouteValueDictionary());
            sb.Append(validat);
            sb.Append("</p>");
            sb.Append("</div>");
            //Javascript
            sb.Append("<script type='text/javascript'>");
            sb.Append("$(function() {");
            sb.Append(" var defaultvalue = $('#Data_" + name + "').val();");
            sb.Append(" $(\"#" + name + "_Radio button[value='\" + defaultvalue + \"']\").addClass(\"active\")");
            sb.Append(" });");
            sb.Append("$('#" + name + "_Radio button').click(function (e) {");
            sb.Append("window.IsValid = true;");
            sb.Append("$('#IsOpenWin_Radio button').removeClass('active');");
            sb.Append("$('#Data_" + name + "').val($(e.currentTarget).addClass('active').attr('value'));");
            sb.Append("return false;");
            sb.Append("});");
            sb.Append("</script>");
            sb.Append("</div>");
            return new MvcHtmlString(sb.ToString());
        }
    }
}
